/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Control;


import persistence.Citys;
import persistence.Movies;
import persistence.Conexion;
import persistence.Subscribers;
import persistence.Locations;
import exceptions.PersistenciaException;
import Interfaces.IDAOs;
import java.util.List;
import objetosNegocio.*;
import persistence.*;

/**
 *
 * @author AlanCarrera
 */
public class Control implements IDAOs {

    private Conexion conexion;
    private String url;
    private String usr;
    private String pas;

    public Control() {
        url = "jdbc:mysql://localhost:3306/cinesmx";
        usr = "root";
        pas = "itson";
    }

    private void prepararConexion() throws PersistenciaException {
        conexion = new Conexion(url, usr, pas);
    }

    @Override
    public List<City> searchCitys() throws PersistenciaException {
        prepararConexion();
        Citys citys = new Citys();
        citys.setConnection(conexion.getConexion());
        return citys.searchCitys();
    }

    @Override
    public List<Subscriber> searchSubscribersByCity(int city) throws PersistenciaException {
        prepararConexion();
        Subscribers subs = new Subscribers();
        subs.setConnection(conexion.getConexion());
        return subs.searchSubscribersByCity(city);
    }

    @Override
    public List<Location> searchLocationsBySub(int city, int subscriber) throws PersistenciaException {
        prepararConexion();
        Locations locations = new Locations();
        locations.setConnection(conexion.getConexion());
        List<Location> locationsCity = locations.searchLocationsByCity(subscriber, city);
        return locationsCity;
    }

    @Override
    public Location searchBillBoardByLocation(int idlocation) throws PersistenciaException {
        prepararConexion();
        BillBoards bbs = new BillBoards();
        bbs.setConnection(conexion.getConexion());
        BillBoard bb = bbs.searchBillboardByLocation(idlocation);
        System.out.println(bb.toString());
        //Recupera Schedules
        prepararConexion();
        Schedules schedules = new Schedules();
        schedules.setConnection(conexion.getConexion());
        List<Schedule> schedulesList = schedules.searchSchedulesByBillboard(bb.getId());
//        bb.setSchedules(schedulesList);
        //Recupera Movie por schedule
        for (int i = 0; i<schedulesList.size(); i++) {
            prepararConexion();
            Movies movies = new Movies();
            movies.setConnection(conexion.getConexion());
            Movie movie = movies.searchMovie(schedulesList.get(i).getMovie().getId());
            schedulesList.get(i).setMovie(movie);
        }
        bb.setSchedules(schedulesList);
        prepararConexion();
        Locations locations = new Locations();
        locations.setConnection(conexion.getConexion());
        Location location = locations.searchLocationById(idlocation);
        location.setBillboard(bb);
        System.out.println(location.toString());
        return location;
    }

    @Override
    public Location searchLocationById(int idLocation) throws PersistenciaException {
        prepararConexion();
        Locations locations = new Locations();
        locations.setConnection(conexion.getConexion());
        Location location = locations.searchLocationById(idLocation);
        return location;
    }

    @Override
    public Movie searchMovieById(int idMovie) throws PersistenciaException {
        prepararConexion();
        Movies movies = new Movies();
        movies.setConnection(conexion.getConexion());
        Movie movie = movies.searchMovie(idMovie);
        return movie;
    }
}
